#include "datarecord.h"

DataRecord::DataRecord(QString fileName, int _recordFlag, int _maxRecordNumber)
{
    recordFlag = _recordFlag;
    maxRecordNumber = _maxRecordNumber;
    record_id = 0;

#if D_WRITE_DATARECORD_DATA_TO_FILE

    char*  ch;
    QByteArray ba = fileName.toLatin1();
    ch=ba.data();

    qDebug()<<"开启:将DATARECORD的数据写入到文件功能!";
    fileObject = fopen( ch ,"w");
    if(fileObject!=NULL){
        fclose(fileObject);
    }

    if ((fileObject=fopen(ch,"a+"))==NULL)//
    {
        qDebug()<< "文件打开失败:" <<ch;
    }
#endif


}

int DataRecord::addItem()
{
    if (0 == recordFlag || record_id > maxRecordNumber)
    {
        return -1;
    }
    record_id ++;
#if D_WRITE_DATARECORD_DATA_TO_FILE
    if ( fileObject !=NULL )
    {
        fprintf( fileObject, "\n"  );
        fprintf( fileObject, "%d , " , record_id );
        fflush( fileObject);
    }
#endif

#if D_WRITE_DATARECORD_DATA_TO_SCREEN
    printf("\n");
    printf("%d, ", record_id);
#endif

    return 1;
}

int DataRecord::writeRecord(double data)
{
    if (0 == recordFlag || record_id > maxRecordNumber)
    {
        return -1;
    }
#if D_WRITE_DATARECORD_DATA_TO_FILE
    if ( fileObject !=NULL )
    {
        fprintf( fileObject, "%lf , " , data );
        fflush( fileObject);
    }
#endif

#if D_WRITE_DATARECORD_DATA_TO_SCREEN
    printf("%lf, ", data);
#endif

    return 1;
}

int DataRecord::writeRecord(float data)
{
    if (0 == recordFlag || record_id > maxRecordNumber)
    {
        return -1;
    }
#if D_WRITE_DATARECORD_DATA_TO_FILE
    if ( fileObject !=NULL )
    {
        fprintf( fileObject, "%f , " , data );
        fflush( fileObject);
    }
#endif

#if D_WRITE_DATARECORD_DATA_TO_SCREEN
    printf("%f, ", data);
#endif

    return 1;
}

int DataRecord::writeRecord(int data)
{
    if (0 == recordFlag || record_id > maxRecordNumber)
    {
        return -1;
    }
#if D_WRITE_DATARECORD_DATA_TO_FILE
    if ( fileObject !=NULL )
    {
        fprintf( fileObject, "%d , " , data );
        fflush( fileObject);
    }
#endif

#if D_WRITE_DATARECORD_DATA_TO_SCREEN
    printf("%d, ", data);
#endif

    return 1;
}

int DataRecord::setRecordOn()
{
    recordFlag = 1;
    return 1;
}

int DataRecord::setRecordOff()
{
    recordFlag = 0;
    return 1;
}

int DataRecord::getCurrentRecordId()
{
    return record_id;
}


